<template>
	<uv-popup ref="popupRef" mode="bottom" round="28rpx">
		<view class="upd-sort">
			<view class="title">修改库存</view>
			<view class="form">
				<view class="zdy-input">
					<view style="flex: 1;">
						<uv-input v-model="queryform.stock" placeholder="请输入库存" type="number"
							:cursor-spacing="cursorSpacing" :border="false" />
					</view>
				</view>
				<view class="tip">
					当前库存: {{ total }}
				</view>
				<view class="btns">
					<button class="cu-btn" @click="handClose">取消</button>
					<button class="cu-btn" @click="handConfirm">确定</button>
				</view>
			</view>
		</view>
	</uv-popup>
</template>

<script setup>
	import {
		reactive,
		ref,
		defineEmits
	} from 'vue'
	import {
		doUpdSort
	} from '@/apis/car.js'
	const emit = defineEmits(['change'])

	const rpxToPx = (rpx) => {
		const screenWidth = uni.getSystemInfoSync().screenWidth
		return (screenWidth * Number.parseInt(rpx)) / 750
	}
	const cursorSpacing = rpxToPx(30)


	const queryform = ref({
		id: '',
		stock: 1
	})
	const total = ref(0)
	const handConfirm = async () => {
		if (!queryform.value.stock) {
			uni.$uv.toast('请输入库存')
			return
		}
		await doUpdSort(queryform.value)
		emit('change', queryform.value)
		handClose()
	}

	const popupRef = ref(null)
	const handOpen = (row) => {
		total.value = row.stock
		queryform.value = {
			id: row.id,
			stock: row.stock
		}
		popupRef.value.open()
	}
	const handClose = () => {
		popupRef.value.close()
	}
	defineExpose({
		handOpen
	})
</script>

<style lang="scss" scoped>
	.upd-sort {
		padding: 30rpx 24rpx;
		padding-bottom: 58rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 58rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 58rpx);

		.title {
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
		}

		.form {
			margin-top: 44rpx;

			.zdy-input {
				height: 94rpx;
				background: #F5F5F5;
				border-radius: 16rpx;
				display: flex;
				align-items: center;

				.place {
					font-size: 30rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
				}
			}

			.tip {
				text-align: right;
				margin-top: 20rpx;
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
			}

			.btns {
				margin-top: 98rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.cu-btn {
					width: 332rpx;
					height: 94rpx;
					background: #F5F5F5;
					border-radius: 16rpx;
					font-size: 30rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;

					&:last-child {
						background: #FFC434;
					}
				}
			}
		}
	}
</style>